package com.ruoyi.caiwu.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.UUID;

@RestController
@CrossOrigin
public class PayController {


    private final String APP_ID = "2021000117696629";
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeg857q4fEECwfu2Cz7SXhnerwyEOH/6imKuhMwtXyzJUwSabnahgUmnG1McC6WOmqhXv7rGj02OzqBd81tF1eTKhAdUus3H21LURCN/0cAEOfUeJ5WZHuilQSBU3Vap8KB3fub9aSIcCGdQdDiJJdvWDs2Wu30paVeRA1S/IP+BHa+/NzDWUhX4y8rpXNSQbgIZ1YpjXoNPRr/NuoNCJJQqImLpQ7F0zUTQ4poAAnF7u9UZBL+oetrkNQU78S0Q3xLdF9mSYaH8/hfvx7fZIFsIPrlX9oLXCoC8RLRpnMy4/Pbx+DkHrXFdsDTHWDIAq4FC+p1P0COPDOfJQJzyO3AgMBAAECggEBAJVCK1WL6KUXTVT7zUzeysKtPiJHkD8o3pbI5EATXf3n4bq8P2q8HbyYyOUC2iUdv7iPRHVwnMugz0R1Dv60vQ4kRHGdgQpd/AhzfUiVC8IoBYSpSAVX7++bVRo64FVKtkJtUeK0cGjdWznATHysxpV50yBygdlvc9d/TWHCB0Y8pXWaYPvmOcUYbvo+suePrFcL3v8oRPSMbPO3Z4wS1IHCRMe4gb/W9SzQZRq5IGVrvyvjKLC8CgYWjygkX62oYhWYYKQls9jq5bX1IxI6ZSM2pf3qcgCD2Q06rE9VRPrxO5PUgAmAnl88jvU+5DlJikq7NftrSHbSUZWJusmBFzECgYEA2ud3rODJ2RyK6bVQpNvFycWPrYclDaXJ5klfD017cV+SqmnPc6hlMjkFfBMeMohQSWJNPf3nMxuIxZWgH8rfKLx+2Du4603YTGLGifadw++ScTIef7mNvi50Rrr3JSiji09ytzxAtvkW5GfKS+UGE2G3YDwphl8GlkSnpL+G68sCgYEAuWCFjQs4DIHOORmL96vMyFbDU/DWKINkwG0WcBZHa3UQNY5NUOi/eWp2akZe6QOkwUkC5F0SvxXqOs3HMfx3ztRHvBUSRKTO26EOx8OEkSOiS59A0g27QtxonXT1E5VjEpNIv1ZvHpgVKBlfZH/vWuxwPRPGtfmyVuvrlXaOAkUCgYB7bPd1amllGUrZ63nE+oXS9qnEVeb2gvwL9GK85hssGY9AtthZhiFI6A3Hn5/vLhzWw655vaeWkXfUsEDQrDqXZXC/yyzAv+utZiQ7tenOiE4xtOita5oYGWlGWDTxprBP5EPyPO7gPCJENqFBmaiLVRVoFYP0V7AHGj7MDZGtcQKBgFydElvi7P+iokr4rmrwVAELB88aFYRsEJvqZDlfiBLxi0WWcJ4pie+49qwluCkl/tvqBB3fOgxpgHoccJZSkfCtMaGPzW6qAAbOMjovPj3c1ap+R74pyhKfV0zDL+W2zqZrGv8syZs5U+iStIvbNtPwKCr7hF1KAIVYnHwxnMUZAoGAaOkNR905tgTLwDlt6WnCoC7RtqX8Xk6Th0Qup2dDsm3hO9c+EGb81ZdjKpNtaL8vkYiwfcESe0jaJ/rJJ7ucURhbi0YyJy3dq63tIWhbu5yX4nx7+XT1ndiu47IgGiRDKcUd7XrqD5ovhZDVr6f/ZhJU1i6GTgBmQd59OvvCSs8=";
    private final String CHARSET = "UTF-8";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqAb++gqjodEV8X9UR3Gs5QWWQ0tdRLf4biduvjpjeX+ho1nadEiDAZGoL3iEV8oC58IPXYtFpgbaxLkYjwsWSkQKOXknedE3a23QHVBzmONWS0bprFW0RSENXdqf1FsBHiFCNquCbxcKneIxmvK7sR7eP98nvLPfrLMc68U70KNR1nhWrBtzl8ehkhWyrZV4n3F/NnrmYkIs3XoSQmRq+M0Hfai7uP72m5boXB30E+37c6UUGCfROkgS5GQ0dcp1/Kct3fOO/+9iWvLKxJoAGaP1wBUTRvRAD9aWwFYhusaLQhqUwu7Y0iOJe9Z8aEhjJoDzTKVRjCDteFO2b6zpRQIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1:4001/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://127.0.0.1:4001/returnUrl";



    @RequestMapping("aliapy")
    public void alipay(HttpServletResponse httpResponse, @RequestParam String htid,@RequestParam String zq ,@RequestParam String je) throws IOException {

        SecureRandom r= new SecureRandom();
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = htid+"|"+zq;
        //付款金额，必填
        String total_amount =je;
        //订单名称，必填
        String subject ="订单";
        //商品描述，可空
        String body = "房租订单号"+htid+"支付宝租房付钱期"+zq+"月在线支付支付";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }


}
